/*
 * B00188409    B00168419   B00210129
 */

package main;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class Nail extends Sprite {

    private boolean active = true;
    
    private int xDirection = 20;
    private int yDirection = 20;

    private static int HEIGHT = 60;
    private static int WIDTH = 14;
    private int[] SEQUENCE;
    private static final int FRATE = 1;
    public Nail (Image image, int w, int h ) throws IOException{
        super(image,w ,h);
        WIDTH  = w;
        HEIGHT = h;
        defineReferencePixel(WIDTH/2,HEIGHT/2);
    };
    public Nail (Image image, int w, int h, int x, int y, int xDirection, int yDirection) throws IOException {
        // initialized nail
        super(image, w, h);
        WIDTH  = w;
        HEIGHT = h;
        setFrameSequence(SEQUENCE);
        defineReferencePixel(WIDTH/2,HEIGHT/2);
        this.xDirection = xDirection;
        this.yDirection = yDirection;
        this.setRefPixelPosition(x, y);        
    }

    public void setSpeed(int speed)
    {
        this.yDirection = speed;
    }

    public int getSpeed()
    {
        return this.yDirection;
    }

    public void advance (int tick) {
        // bullet rate
     if (tick%FRATE==0 && active ==true)
        if (this.getRefPixelY()>0)
            this.move(xDirection, -yDirection);
        else {
            super.setVisible(false);
            active = false;
        }
     }

    public void shot(int w, int h) {
        active = true;
        this.setRefPixelPosition(w, h);
        this.setVisible(true);
    }

     public boolean isAppear () {
         return active;
     }
     public void destroy () {
         super.setVisible(false);
         active = false;
     }
}
